{% extends "admin/layout.html" %}

{% block title %}资产统计 - {{ system_config.site_name|default('项目管理系统') }}{% endblock %}

{% block content %}
<div class="container-fluid">
    <div class="d-flex justify-content-between align-items-center mb-4">
        <div>
            <h1 class="h3 mb-0">资产统计分析</h1>

        </div>
        <div class="btn-group">
            <button type="button" class="btn btn-outline-primary" onclick="refreshStats()">
                <i class="fas fa-sync-alt me-1"></i> 刷新数据
            </button>
            <a href="{{ url_for('asset.export_assets') }}" class="btn btn-outline-success">
                <i class="fas fa-download me-1"></i> 导出报表
            </a>
        </div>
    </div>

    <!-- 关键指标 -->
    <div class="row mb-4">
        <div class="col-xl-3 col-md-6 mb-4">
            <div class="card stat-card border-left-primary h-100">
                <div class="card-body">
                    <div class="row no-gutters align-items-center">
                        <div class="col mr-2">
                            <div class="text-xs font-weight-bold text-primary text-uppercase mb-1">总资产数量</div>
                            <div class="h5 mb-0 font-weight-bold text-gray-800">{{ stats.total_assets|default(0) }}</div>
                        </div>
                        <div class="col-auto">
                            <i class="fas fa-laptop fa-2x text-gray-300"></i>
                        </div>
                    </div>
                </div>
            </div>
        </div>
        <div class="col-xl-3 col-md-6 mb-4">
            <div class="card stat-card border-left-success h-100">
                <div class="card-body">
                    <div class="row no-gutters align-items-center">
                        <div class="col mr-2">
                            <div class="text-xs font-weight-bold text-success text-uppercase mb-1">资产总价值</div>
                            <div class="h5 mb-0 font-weight-bold text-gray-800">¥{{ "%.2f"|format(stats.total_value|default(0)) }}</div>
                        </div>
                        <div class="col-auto">
                            <i class="fas fa-dollar-sign fa-2x text-gray-300"></i>
                        </div>
                    </div>
                </div>
            </div>
        </div>
        <div class="col-xl-3 col-md-6 mb-4">
            <div class="card stat-card border-left-warning h-100">
                <div class="card-body">
                    <div class="row no-gutters align-items-center">
                        <div class="col mr-2">
                            <div class="text-xs font-weight-bold text-warning text-uppercase mb-1">即将过保</div>
                            <div class="h5 mb-0 font-weight-bold text-gray-800">{{ stats.expiring_soon|default(0) }}</div>
                        </div>
                        <div class="col-auto">
                            <i class="fas fa-exclamation-triangle fa-2x text-gray-300"></i>
                        </div>
                    </div>
                </div>
            </div>
        </div>
        <div class="col-xl-3 col-md-6 mb-4">
            <div class="card stat-card border-left-info h-100">
                <div class="card-body">
                    <div class="row no-gutters align-items-center">
                        <div class="col mr-2">
                            <div class="text-xs font-weight-bold text-info text-uppercase mb-1">需维护</div>
                            <div class="h5 mb-0 font-weight-bold text-gray-800">{{ stats.need_maintenance|default(0) }}</div>
                        </div>
                        <div class="col-auto">
                            <i class="fas fa-tools fa-2x text-gray-300"></i>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>

    <div class="row">
        <!-- 状态分布 -->
        <div class="col-lg-6 mb-4">
            <div class="card h-100">
                <div class="card-header">
                    <h5 class="card-title mb-0">资产状态分布</h5>
                </div>
                <div class="card-body">
                    {% if stats.status_distribution %}
                    <div class="chart-pie pt-4 pb-2">
                        <canvas id="statusChart" width="100%" height="200"></canvas>
                    </div>
                    <div class="mt-4 text-center small">
                        {% for status, count in stats.status_distribution.items() %}
                        <span class="me-3">
                            <i class="fas fa-circle 
                                {% if status == 'in_use' %}text-success
                                {% elif status == 'available' %}text-primary
                                {% elif status == 'maintenance' %}text-warning
                                {% elif status == 'retired' %}text-secondary
                                {% else %}text-gray-300{% endif %}"></i> 
                            {{ 
                                '使用中' if status == 'in_use'
                                else '可用' if status == 'available'
                                else '维护中' if status == 'maintenance'
                                else '已报废' if status == 'retired'
                                else status
                            }} ({{ count }})
                        </span>
                        {% endfor %}
                    </div>
                    {% else %}
                    <div class="text-center py-4 text-muted">
                        <i class="fas fa-chart-pie fa-2x mb-2"></i>
                        <p>暂无状态分布数据</p>
                    </div>
                    {% endif %}
                </div>
            </div>
        </div>

        <!-- 类别分布 -->
        <div class="col-lg-6 mb-4">
            <div class="card h-100">
                <div class="card-header">
                    <h5 class="card-title mb-0">资产类别分布</h5>
                </div>
                <div class="card-body">
                    {% if stats.category_distribution %}
                    <div class="chart-bar pt-4 pb-2">
                        <canvas id="categoryChart" width="100%" height="200"></canvas>
                    </div>
                    {% else %}
                    <div class="text-center py-4 text-muted">
                        <i class="fas fa-chart-bar fa-2x mb-2"></i>
                        <p>暂无类别分布数据</p>
                    </div>
                    {% endif %}
                </div>
            </div>
        </div>
    </div>

    <!-- 详细统计表格 -->
    <div class="row">
        <div class="col-12">
            <div class="card">
                <div class="card-header">
                    <h5 class="card-title mb-0">详细统计</h5>
                </div>
                <div class="card-body">
                    <div class="table-responsive">
                        <table class="table table-bordered">
                            <thead class="table-light">
                                <tr>
                                    <th>统计项目</th>
                                    <th>数量</th>
                                    <th>占比</th>
                                    <th>总价值</th>
                                    <th>平均价值</th>
                                </tr>
                            </thead>
                            <tbody>
                                <!-- 这里可以添加更详细的统计行 -->
                                <tr>
                                    <td>总资产</td>
                                    <td>{{ stats.total_assets|default(0) }}</td>
                                    <td>100%</td>
                                    <td>¥{{ "%.2f"|format(stats.total_value|default(0)) }}</td>
                                    <td>
                                        {% if stats.total_assets and stats.total_assets > 0 %}
                                        ¥{{ "%.2f"|format(stats.total_value / stats.total_assets) }}
                                        {% else %}
                                        -
                                        {% endif %}
                                    </td>
                                </tr>
                                {% if stats.status_distribution %}
                                {% for status, count in stats.status_distribution.items() %}
                                <tr>
                                    <td>
                                        {{ 
                                            '使用中' if status == 'in_use'
                                            else '可用' if status == 'available'
                                            else '维护中' if status == 'maintenance'
                                            else '已报废' if status == 'retired'
                                            else status
                                        }}
                                    </td>
                                    <td>{{ count }}</td>
                                    <td>
                                        {% if stats.total_assets and stats.total_assets > 0 %}
                                        {{ "%.1f"|format(count / stats.total_assets * 100) }}%
                                        {% else %}
                                        0%
                                        {% endif %}
                                    </td>
                                    <td>-</td>
                                    <td>-</td>
                                </tr>
                                {% endfor %}
                                {% endif %}
                            </tbody>
                        </table>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>

<!-- 引入Chart.js -->
<script src="https://cdn.jsdelivr.net/npm/chart.js"></script>

<script>
function refreshStats() {
    location.reload();
}

// 初始化图表
document.addEventListener('DOMContentLoaded', function() {
    // 状态分布饼图
    {% if stats.status_distribution %}
    const statusCtx = document.getElementById('statusChart').getContext('2d');
    const statusChart = new Chart(statusCtx, {
        type: 'pie',
        data: {
            labels: [
                {% for status, count in stats.status_distribution.items() %}
                '{{ 
                    "使用中" if status == "in_use"
                    else "可用" if status == "available"
                    else "维护中" if status == "maintenance"
                    else "已报废" if status == "retired"
                    else status
                }}',
                {% endfor %}
            ],
            datasets: [{
                data: [
                    {% for status, count in stats.status_distribution.items() %}
                    {{ count }},
                    {% endfor %}
                ],
                backgroundColor: [
                    '#1cc88a', '#4e73df', '#f6c23e', '#858796', '#36b9cc', '#e74a3b'
                ],
                hoverBackgroundColor: [
                    '#17a673', '#2e59d9', '#d4a72c', '#6c757d', '#2c9faf', '#be2617'
                ],
                hoverBorderColor: "rgba(234, 236, 244, 1)",
            }],
        },
        options: {
            maintainAspectRatio: false,
            plugins: {
                legend: {
                    display: false
                }
            },
            cutout: '50%',
        },
    });
    {% endif %}

    // 类别分布柱状图
    {% if stats.category_distribution %}
    const categoryCtx = document.getElementById('categoryChart').getContext('2d');
    const categoryChart = new Chart(categoryCtx, {
        type: 'bar',
        data: {
            labels: [
                {% for category, count in stats.category_distribution.items() %}
                '{{ category }}',
                {% endfor %}
            ],
            datasets: [{
                label: "资产数量",
                backgroundColor: "#4e73df",
                hoverBackgroundColor: "#2e59d9",
                borderColor: "#4e73df",
                data: [
                    {% for category, count in stats.category_distribution.items() %}
                    {{ count }},
                    {% endfor %}
                ],
            }],
        },
        options: {
            maintainAspectRatio: false,
            scales: {
                x: {
                    grid: {
                        display: false
                    },
                    ticks: {
                        maxTicksLimit: 6
                    }
                },
                y: {
                    beginAtZero: true,
                    ticks: {
                        maxTicksLimit: 5
                    },
                    grid: {
                        color: "rgb(234, 236, 244)",
                    }
                }
            },
        },
    });
    {% endif %}
});
</script>
{% endblock %}